package cn.hexcloud.dga.meta.controller;

import cn.hexcloud.dga.lineage.service.GovernanceLineageTableService;
import cn.hexcloud.dga.meta.bean.TableMetaInfo;
import cn.hexcloud.dga.meta.bean.TableMetaInfoExtra;
import cn.hexcloud.dga.meta.bean.TableMetaInfoForQuery;
import cn.hexcloud.dga.meta.bean.TableMetaInfoVO;
import cn.hexcloud.dga.meta.service.TableMetaInfoExtraService;
import cn.hexcloud.dga.meta.service.TableMetaInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表 前端控制器
 * </p>
 *
 * @author jiangdan7
 * @since 2023-08-23
 */
@RestController
@RequestMapping("/tableMetaInfo")
public class TableMetaInfoController {
    @Autowired
    TableMetaInfoService tableMetaInfoService;

    @Autowired
    TableMetaInfoExtraService tableMetaInfoExtraService;

    @Autowired
    GovernanceLineageTableService governanceLineageTableService;

    @Value("${hive.schemaNames}")
    String schemaName;

    @PostMapping("/init-tables/{schema_name}/{init_date}")
    @CrossOrigin
    public void initTable(@PathVariable("schema_name") String schemaName,@PathVariable("init_date") String initDate) {
        tableMetaInfoService.initTableMetaInfo(initDate, schemaName);
        governanceLineageTableService.initLineage(initDate);
    }

    @GetMapping("/table-list")
    @CrossOrigin
    public String tableList(TableMetaInfoForQuery tableMetaInfoForQuery){
        Integer total = tableMetaInfoService.getTableMetaInfoCount(tableMetaInfoForQuery);
        List<TableMetaInfoVO> tableMetaInfoVOList = tableMetaInfoService.getTableMetaInfoList(tableMetaInfoForQuery);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",total);
        jsonObject.put("list",tableMetaInfoVOList);
        return jsonObject.toJSONString();
    }

    @GetMapping("/table/{tableMetaInfoId}")
    public TableMetaInfo tableDetail(@PathVariable("tableMetaInfoId") String tableMetaInfoId){
        return tableMetaInfoService.getTableMetaInfoAll(Long.parseLong(tableMetaInfoId));
    }

    @PostMapping("/tableExtra")
    public String saveTableMetaInfoExtra(@RequestBody TableMetaInfoExtra tableMetaInfoExtra){
        tableMetaInfoExtraService.saveTableMetaInfoExtra(tableMetaInfoExtra);
        return "success";
    }
}
